package cn.com.dhcc.turbo.service.exch.cmdata;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.dao.exch.CarvedDiscsUploadDataDao;
import cn.com.dhcc.turbo.entity.exch.CarvedDiscsUploadData;
import cn.com.dhcc.turbo.entity.exch.SendCmData;
import cn.com.dhcc.turbo.entity.exch.SendFileData;
import cn.com.dhcc.turbo.service.exch.filedata.SendFileDataService;

@Service
public class CarvedDiscsUploadDataService extends AbstractCrudService<CarvedDiscsUploadData> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

	@Autowired
	private CarvedDiscsUploadDataDao dao;
	@Autowired
	private SendFileDataService sendFileDataService;
	
	@Autowired
	private SendCmDataService sendCmDataService;
	
	@Override
	public CrudDao<CarvedDiscsUploadData> getCrudDao() {
		return dao;
	}

	/**
	 * 获取上传文件的发送状态
	 * CarvedDiscsUploadData的主键id，多个用逗号分隔
	 * @return
	 */
	public String getStatus(String id) throws ServiceException{
		if (id==null || "".equals(id)) {
			return null;
		}
		String[] ids=id.split(",");
		WhereCondition parameter=new WhereCondition();
		parameter.where().in("id", ids);
		String status="0";
		try {
			List<CarvedDiscsUploadData> list=this.list(parameter);
			if (list!=null && !list.isEmpty()) {
				for (CarvedDiscsUploadData carvedDiscsUploadData : list) {
					if (carvedDiscsUploadData.getDataType().equals(AppConst.ExchDataType.FILE_DATA.value())) {//如果数据类型是非结构化数据
						status=getFileDataStatus(carvedDiscsUploadData.getBatchId());
					}else{
						status=getCmDataStatus(carvedDiscsUploadData.getBatchId());
					}
					
					if (status.equals(AppConst.UploadProcessStatus.DATA_SENDING.value())) {
						return AppConst.UploadProcessStatus.DATA_SENDING.value();
					}else if(status.equals(AppConst.UploadProcessStatus.DATA_PART_SENT.value())){
						return AppConst.UploadProcessStatus.DATA_PART_SENT.value();
					}
				}
				return AppConst.UploadProcessStatus.DATA_SENT.value();
			}
		} catch (ServiceException e) {
			logger.error("获取状态异常:"+e.getMessage());
			throw new ServiceException("获取状态异常:"+e.getMessage());
		}
		return AppConst.UploadProcessStatus.DATA_SENDING.value();
		
	}
	private String getFileDataStatus(String groupId) throws ServiceException{
		String status=AppConst.UploadProcessStatus.DATA_SENDING.value();
		WhereCondition parameter=new WhereCondition();
		WhereCondition sumParameter=new WhereCondition();
		try {
			parameter.where().eq("group_id", groupId).and().eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value());
			sumParameter.where().eq("group_id", groupId);
			List<SendFileData> list=sendFileDataService.list(parameter);
			List<SendFileData> sumList=sendFileDataService.list(sumParameter);
			if (list!=null && list.size()==sumList.size()) {
				status=AppConst.UploadProcessStatus.DATA_SENDING.value();
				return status;
			}
			
			parameter=new WhereCondition();
			parameter.where().eq("group_id", groupId).and().eq("SEND_STATUS", AppConst.SendStatus.SENT.value());
			list=sendFileDataService.list(parameter);
			if (list!=null && list.size()==sumList.size()) {
				status=AppConst.UploadProcessStatus.DATA_SENT.value();
				return status;
			}
			
			return AppConst.UploadProcessStatus.DATA_PART_SENT.value();
			
		} catch (ServiceException e) {
			logger.error("获取状态异常:"+e.getMessage());
			throw new ServiceException("获取状态异常:"+e.getMessage());
		}
	}
	
	private String getCmDataStatus(String pid) throws ServiceException{
		String status=AppConst.UploadProcessStatus.DATA_SENDING.value();
		WhereCondition parameter=new WhereCondition();
		WhereCondition sumParameter=new WhereCondition();
		try {
			parameter.where().eq("pid", pid).and().eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value());
			sumParameter.where().eq("pid", pid);
			List<SendCmData> list=sendCmDataService.list(parameter);
			List<SendCmData> sumList=sendCmDataService.list(sumParameter);
			if (list!=null && list.size()==sumList.size()) {
				status=AppConst.UploadProcessStatus.DATA_SENDING.value();
				return status;
			}
			
			parameter=new WhereCondition();
			parameter.where().eq("pid", pid).and().eq("SEND_STATUS", AppConst.SendStatus.SENT.value());
			list=sendCmDataService.list(parameter);
			if (list!=null && list.size()==sumList.size()) {
				status=AppConst.UploadProcessStatus.DATA_SENT.value();
				return status;
			}
			
			return AppConst.UploadProcessStatus.DATA_PART_SENT.value();
			
		} catch (ServiceException e) {
			logger.error("获取状态异常:"+e.getMessage());
			throw new ServiceException("获取状态异常:"+e.getMessage());
		}
	}
	
}
